package controllers

import (
	"beego.56/models"
	beego "github.com/beego/beego/v2/server/web"
	"math"
)

type Bee struct {
	beego.Controller
}
type Res struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *Bee) Add() {
	name := c.GetString("name")
	Tel := c.GetString("tel")
	Yu := c.GetString("yu")
	Ks := c.GetString("ks")
	People := c.GetString("people")
	be := models.Ys{
		Name:   name,
		Tel:    Tel,
		Yu:     Yu,
		Ks:     Ks,
		People: People,
	}
	err := models.DB.Create(&be).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Res{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
func (c *Bee) List() {
	query := models.DB.Model(&models.Ys{})
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 3)
	var count int64
	query.Count(&count)
	sum := int(math.Ceil(float64(count) / float64(size)))
	offset := (page - 1) * size
	var bb []models.Ys
	err := query.Limit(size).Offset(offset).Find(&bb).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  " 查询失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		pageData := make(map[string]interface{})
		pageData["bb"] = bb
		pageData["sum"] = sum
		c.Data["json"] = Res{
			Code: 200,
			Msg:  " 查询成功",
			Data: pageData,
		}
		c.ServeJSON()
	}
}
